package com.djsearch.ui;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.djsearch.R;
import com.djsearch.bean.ForPurchase;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.controller.AsyncHandler;
import com.djsearch.controller.IRequest;
import com.djsearch.controller.IResponse;
import com.djsearch.controller.JSONReq;
import com.djsearch.controller.JSONResp;
import com.djsearch.data.ImageManager;
import com.djsearch.util.AsyncImageLoader;
import com.djsearch.util.AsyncImageLoader.ImageCallback;

/**
 * 淘购页面详情
 * @author chenyang
 *
 */
public class TaoGouDetailsActivity extends BaseActivity {

	private Context mContext;
	private ForPurchase taogouGoods;
	private ImageView goodsImg;
	private TextView gongSiDescribe,goodsDescribe,leaveDaysText
	,buyPersonsText,nowPriceText,oldPriceText,buyBtn;
	private ProgressBar progressBar;
	private AsyncImageLoader asyncImageLoader = new AsyncImageLoader("bigPic");
	private Button shouchangBtn,shareBtn;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.taogou_details_layout);
		mContext = this;
		//接收从上个页面传递来的对象
		taogouGoods = (ForPurchase) getIntent().getSerializableExtra("data");
		//初始化主界面
		initView();
		//初始化主界面相关点击事件
		initOnClick();
		//
		initHandler();
	}
	@Override
	void initHandler() {
		// TODO Auto-generated method stub

	}

	@Override
	void initView() {
		// TODO Auto-generated method stub
		shouchangBtn = (Button)findViewById(R.id.shouchangBtn);
		shareBtn = (Button)findViewById(R.id.shareBtn);
		buyBtn = (Button)findViewById(R.id.buyBtn);//立即购买
		goodsImg = (ImageView)findViewById(R.id.goodsImg);//商品大图
		progressBar = (ProgressBar)findViewById(R.id.progressBar);
		asyncImageLoader.loadDrawable(progressBar,goodsImg,
				taogouGoods.getBigImage(),new ImageCallback(){
			@Override
			public void imageLoaded(ProgressBar progressBar,ImageView imageView,
					Drawable imageDrawable, String imageUrl, String tag) {
				// TODO Auto-generated method stub
				imageView.setImageBitmap(ImageManager.drawableToBitmap(imageDrawable,400.0f));
				progressBar.setVisibility(View.GONE);
			}
			
		});
		gongSiDescribe = (TextView)findViewById(R.id.gongSiDescribe);//公司描述
		gongSiDescribe.setText(taogouGoods.getDescription());
		goodsDescribe = (TextView)findViewById(R.id.goodsDescribe);//商品描述
		goodsDescribe.setText(taogouGoods.getDescription());
		leaveDaysText = (TextView)findViewById(R.id.leaveDaysText);//剩余天数
		buyPersonsText = (TextView)findViewById(R.id.buyPersonsText);//购买人数
		buyPersonsText.setText(taogouGoods.getHasBuy()+"人已购买");
		nowPriceText = (TextView)findViewById(R.id.nowPriceText);//现在价格
		nowPriceText.setText(taogouGoods.getNowPrice());
		oldPriceText = (TextView)findViewById(R.id.nowPriceText);//原价
		oldPriceText.setText(taogouGoods.getSalePrice());
	}

	@Override
	void initOnClick() {
		// TODO Auto-generated method stub

		buyBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//去购买
				Intent intent=new Intent();
		        intent.setClass(mContext, WebViewActivity.class);
		        intent.putExtra("url", taogouGoods.getShopUrl());
		        startActivity(intent);
		        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
			}
		});
		shareBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//分享
				Intent intent = new Intent(mContext,ShareToWeiboActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("shareContent", taogouGoods.getDescription());
				bundle.putString("goodsUrl", taogouGoods.getShopUrl());
				intent.putExtras(bundle);
				startActivity(intent);
				overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
//                share(taogouGoods.getDescription()+":"+ taogouGoods.getShopUrl());
			}
		});
		
		shouchangBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isLogin()) {
					requestShouChang(initRequestMap(taogouGoods));
				} else {
					Toast.makeText(mContext, "你未登录，不能搜藏", 5000).show();
				}
				
			}
		});
	}
	
	
	
	/**
	 * 请求收藏
	 * data参数说明：{“userId”:”8765”,“name”:”大酒店”,”smallImage”:http://image.jpg,
       ”bigImage”:”http://image.jpg”,”place”:”地点”,
       ”telephone”:”0373-5678”,
       ”shopUrl”:http://www.baidu.com,”description”:”描述”}
	 */
	private void requestShouChang(Map<String, String> map) {

		IRequest request = new JSONReq("collect",map);
		HTTP.post(request, new AsyncHandler(){
			@Override
			public void onStart() {
				showDialog("正在收藏...");
			}
			@Override
			public void onSuccess(JSONResp response) {
				// TODO Auto-generated method stub
				int result = response.result();
				if(IResponse.Success==result){
						getDataToResolve(response);
				} else if (IResponse.Fail==result) {
					Toast.makeText(mContext, "收藏失败", 5000).show();
				}
					missDialog();
			}
			@Override
			public void onFailure(Throwable e, String response) {
				// TODO Auto-generated method stub
				missDialog();
			}
			
		});
	
	}
	
	
	
	private Map<String, String> initRequestMap(ForPurchase taoGouGoods) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("userId", preferences.getString("userId", ""));//用户id
		map.put("name", taoGouGoods.getShopName());//商品名字
		map.put("smallImage", taoGouGoods.getSmallImage());//小图片
		map.put("bigImage", taoGouGoods.getBigImage());//大图片
		map.put("place", "");//地点
		map.put("telephone", "");//电话
		map.put("shopUrl", taoGouGoods.getShopUrl());//商家地址
		map.put("description", taoGouGoods.getDescription());//描述
		
		return map;
	}
	
//	/**
//	 * 点击返回键
//	 */
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		// TODO Auto-generated method stub
//		startActivity(mContext, TaoGouActivity.class, 1);
//		return super.onKeyDown(keyCode, event);
//	}
	@Override
	void getDataToResolve(JSONResp response) {
		// TODO Auto-generated method stub
		
	}
}
